package com.haierp.controller.bi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.haierp.model.bi.BiItem;
import com.haierp.model.bi.BiPurchaseSiteSuccess;
import com.haierp.model.bi.BiPurchaseStorage;
import com.haierp.model.bi.BiPurchaseSuccessRate;
import com.haierp.model.bi.BiUpToNew;
import com.haierp.service.bi.IBiItemService;
import com.haierp.util.NumberUtil;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.shiro.ShiroUser;

@Controller
@RequestMapping("/bi/item")
public class BiItemController extends BaseController {
	@Autowired
	private IBiItemService biItemService;
	
	@RequestMapping("/selectBiItemListingReportByGmtCreate")
	public String selectBiItemListingReportByGmtCreate(HttpServletRequest request) {
		List<BiUpToNew> biItemList = biItemService.selectBiItemListingReportByGmtCreate();
		request.setAttribute("biItemList", biItemList);
		return "bi/itemReport";
	}
	
	@RequestMapping("/selectBiStorageSkuReportByGmtCreate")
	public String selectBiStorageSkuReportByGmtCreate(HttpServletRequest request) {
		ShiroUser shiroUser = this.getShiroUser();
		List<BiPurchaseStorage> biStorageSkuList = biItemService.selectBiStorageSkuReportByGmtCreate(shiroUser.getCompanyId());
		request.setAttribute("biStorageSkuList", biStorageSkuList);
		return "bi/storageSkuReport";
	}

	@RequestMapping("/selectBiPurchasingSuccessRateByMouth")
	public String selectBiPurchasingSuccessRateByMouth(HttpServletRequest request) {
		List<BiPurchaseSuccessRate> purchaseSuccessRateDay = biItemService.selectBiPurchasingSuccessRateByDay();
		purchaseSuccessRateDay.forEach(purchase -> {
			purchase.setPurchaseSuccessRate(NumberUtil.formatDoubleTwo(purchase.getPurchaseSuccessRate()*100));
		});
		List<BiPurchaseSuccessRate> purchaseSuccessRatemonth = biItemService.selectBiPurchasingSuccessRateByMonth();
		purchaseSuccessRatemonth.forEach(purchase -> {
			purchase.setPurchaseSuccessRate(NumberUtil.formatDoubleTwo(purchase.getPurchaseSuccessRate()*100));
		});
		request.setAttribute("purchaseSuccessRateDay", purchaseSuccessRateDay);
		request.setAttribute("purchaseSuccessRatemonth", purchaseSuccessRatemonth);
		return "bi/purchaseSuccessRateReport";
	}

	@RequestMapping("/selectBiPurchasingBuysiteSuccessByMouth")
	public String selectBiPurchasingBuysiteSuccessByMouth(HttpServletRequest request) {
		Map<String, BiPurchaseSiteSuccess> shipMapMouth = new HashMap<String,BiPurchaseSiteSuccess>();
		Map<String, BiPurchaseSiteSuccess> shipMapWeek = new HashMap<String,BiPurchaseSiteSuccess>();
		List<BiPurchaseSiteSuccess> purchaseSuccessBuysite = biItemService.selectBiPurchasingBuysiteSuccessByMouth();
		purchaseSuccessBuysite.forEach(purchase -> {
			purchase.setPurchaseSiteSuccess((NumberUtil.formatDoubleTwo(purchase.getPurchaseSiteSuccess()*100)));
			if(shipMapMouth.containsKey(purchase.getDimensionDate())) {
				BiPurchaseSiteSuccess biPurchaseSiteSuccess = shipMapMouth.get(purchase.getDimensionDate());
				biPurchaseSiteSuccess.setCount(biPurchaseSiteSuccess.getCount()+1);
			} else {
				BiPurchaseSiteSuccess biPurchaseSiteSuccess = new BiPurchaseSiteSuccess();
				biPurchaseSiteSuccess.setCount(1);
				shipMapMouth.put(purchase.getDimensionDate(), biPurchaseSiteSuccess);
			}
		});
		List<BiPurchaseSiteSuccess> purchaseSuccessBuysiteWeek = biItemService.selectBiPurchasingBuysiteSuccessByWeek();
		purchaseSuccessBuysiteWeek.forEach(purchase -> {
			purchase.setPurchaseSiteSuccess((NumberUtil.formatDoubleTwo(purchase.getPurchaseSiteSuccess()*100)));
			if(shipMapWeek.containsKey(purchase.getDimensionDate())) {
				BiPurchaseSiteSuccess biPurchaseSiteSuccess = shipMapWeek.get(purchase.getDimensionDate());
				biPurchaseSiteSuccess.setCount(biPurchaseSiteSuccess.getCount()+1);
			} else {
				BiPurchaseSiteSuccess biPurchaseSiteSuccess = new BiPurchaseSiteSuccess();
				biPurchaseSiteSuccess.setCount(1);
				shipMapWeek.put(purchase.getDimensionDate(), biPurchaseSiteSuccess);
			}
		});
		request.setAttribute("shipMapMouth", shipMapMouth);
		request.setAttribute("shipMapWeek", shipMapWeek);
		request.setAttribute("purchaseSuccessBuysite", purchaseSuccessBuysite);
		request.setAttribute("purchaseSuccessBuysiteWeek", purchaseSuccessBuysiteWeek);
		return "bi/purchaseSuccessBuysiteReport";
	}

	@RequestMapping("/selectAccessToSmallPrograms")
	public String selectAccessToSmallPrograms(HttpServletRequest request) {
		int totalNumber = 0;
		List<BiItem> accessToProgramsList = biItemService.selectAccessToSmallPrograms();
		for (BiItem biItem : accessToProgramsList) {
			totalNumber += biItem.getWxForward()+biItem.getScanCode()+biItem.getShareCode();
		}
		request.setAttribute("totalNumber", totalNumber);
		request.setAttribute("accessToProgramsList", accessToProgramsList);
		return "bi/accessToProgramReport";
	}

	@RequestMapping("/selectCollectByItemId")
	public String selectCollectByItemId(HttpServletRequest request) {
		List<BiItem> collectByItemIdList = biItemService.selectCollectByItemId();
		request.setAttribute("collectByItemIdList", collectByItemIdList);
		return "bi/collectionOfGoods";
	}
}
